﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Arcanea.Mediator.XmlClasses;

namespace Arcanea.Mediator
{
    /// <summary>
    /// Module de gestion des types dans le programme
    /// </summary>
    public class TypeDatabase
    {
        private static Type[] allBaseType = new Type[] { 
                typeof(Boolean), typeof(Byte),
                typeof(Int16), typeof(Int32),
                typeof(Int64), typeof(Single),
                typeof(Decimal), typeof(Double),
                typeof(String), typeof(DateTime)
        };
        /// <summary>
        /// Retourne la liste des Types de base supportés par le programme
        /// </summary>
        public static Type[] AllBaseType
        {
            get { return allBaseType; }
        }


        private static String[] allBaseTypeStrings = null;
        /// <summary>
        /// Retourne la liste des Types de base supportés par le programme au format string
        /// </summary>
        public static String[] AllBaseTypeStrings
        {
            get
            {
                if (allBaseTypeStrings == null)
                    allBaseTypeStrings = GetDisponibleBaseType().ToArray();
                return allBaseTypeStrings;
            }
        }


        private static List<string> GetDisponibleBaseType()
        {
            List<string> result = new List<string>();

            for (int i = 0; i < allBaseType.Length; i++)
            {
                result.Add(allBaseType[i].ToString());
            }

            return result;

        }

        /// <summary>
        /// A partir d'une string de type, retourne le type Sql associé.
        /// </summary>
        /// <param name="type">String du type à convertir</param>
        /// <returns>le type Sql trouvé</returns>
        public static string GetSqlType(string type)
        {
            string result = null;
            if (type == TypeDatabase.AllBaseTypeStrings[0]) //Bool
            {
                result = "BOOLEAN";
            }
            else if (type == TypeDatabase.AllBaseTypeStrings[1]) //Byte
            {
                result = "TINYINT";
            }
            else if (type == TypeDatabase.AllBaseTypeStrings[2]) //Int16
            {
                result = "SMALLINT";
            }
            else if (type == TypeDatabase.AllBaseTypeStrings[3]) //Int32
            {
                result = "INT";
            }
            else if (type == TypeDatabase.AllBaseTypeStrings[4]) //Int64
            {
                result = "BIGINT";
            }
            else if (type == TypeDatabase.AllBaseTypeStrings[5]) //Single
            {
                result = "FLOAT";
            }
            else if (type == TypeDatabase.AllBaseTypeStrings[6]) //Decimal
            {
                result = "DECIMAL";
            }
            else if (type == TypeDatabase.AllBaseTypeStrings[7]) //Double
            {
                result = "DOUBLE";
            }
            else if (type == TypeDatabase.AllBaseTypeStrings[8]) //String
            {
                result = "TEXT";
            }
            else if (type == TypeDatabase.AllBaseTypeStrings[9]) //DateTime
            {
                result = "DATETIME";
            }
            else
            {
                //Cas d'une clée étrangère
                result = "INT";
            }

            return result;
        }
    
    }
}
